<template>
  <el-dialog :model-value="show" title="选择品牌" @close="emit('hide')">
    <el-form ref="formRef" :model="params">
      <el-form-item>
        <el-input
          v-model="params.keyword"
          autocomplete="off"
          placeholder="品牌名称搜索"
        />
        <el-button :icon="Search" @click="searchHandle"> </el-button>
      </el-form-item>
    </el-form>
    <el-table
      select-on-indeterminate
      table-layout="auto"
      cell-class-name="align-c"
      :data="list"
      @selection-change="selectChange"
      border
    >
      <el-table-column align="center" type="selection"></el-table-column>
      <el-table-column
        align="center"
        label="品牌名称"
        prop="title"
      ></el-table-column>
      <el-table-column align="center" label="所属专题">
        <template #default="{ row }">
          <span>{{ row.categoryName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="添加时间">
        <template #default="{ row }">
          <span>{{ formateDate(row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancelFn">取消</el-button>
        <el-button type="primary" @click="submitFn">确定</el-button>
      </span>
    </template>
    <div class="page">
      <ElConfigProvider :locale="zhCn">
        <el-pagination
          v-model:current-page="params.pageNum"
          v-model:page-size="params.pageSize"
          :page-sizes="[5, 10, 20, 50, 100]"
          background
          layout="  prev, pager, next"
          :total="total"
          @size-change="pageChangeHandle"
          @current-change="pageSizeChangeHandle"
        />
      </ElConfigProvider>
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { subjectListApi, createRecommendSubjectApi } from "../api";
import { ElConfigProvider, ElMessage, ElMessageBox } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { ref } from "vue";
import useTable from "@/hooks/useTable";
import { Search } from "@element-plus/icons-vue";
import { formateDate } from "@/utils/time";

const addArr = ref<ICreateSubjectParams[]>([]);
const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
});
const params = ref({
  keyword: "",
  pageNum: 1,
  pageSize: 5,
});
const emit = defineEmits(["hide"]);

const { list, total, pageChangeHandle, pageSizeChangeHandle, searchHandle } =
  useTable(subjectListApi, params.value);

//单独一个取消选择
const selectChange = (selection: ISubjectList[]) => {
  console.log("selection:", selection);
  addArr.value = selection.map((item) => ({
    subjectId: item.id as number,
    subjectName: item.title as string,
  }));
};
//取消按钮
const cancelFn = () => {
  emit("hide", true);
};
//确认按钮
const submitFn = () => {
  console.log(addArr.value);
  ElMessageBox.confirm("确定要添加吗？", "添加", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      if (addArr.value.length === 0) {
        ElMessage.warning("请至少选择一条");
      } else {
        createRecommendSubjectApi(addArr.value).then((res) => {
          if (res.code === 200) {
            ElMessage.success("添加成功");
            emit("hide", true);
            addArr.value = [];
            params.value.keyword = "";
          }
        });
      }
    })
    .catch(() => {});
};
</script>

<style scoped lang="less">
:deep(.align-c) {
  .cell {
    text-align: center;
  }
}
:deep(.el-input) {
  width: 200px;
}
.page {
  display: flex;
  flex-direction: row-reverse;
  margin-top: 20px;
}
</style>
